unit UCOI0010P3;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UGlobal,
  cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, cxCheckComboBox, cxContainer,
  Datasnap.DBClient, Vcl.StdCtrls, cxButtonEdit, cxDropDownEdit, cxMaskEdit,
  cxImageComboBox, Vcl.Buttons, fsButton, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid,
   Vcl.ExtCtrls, fsPanel, cxGridBandedTableView, cxGridDBBandedTableView,
  UFrameChkCombo, UFrameCode, fsLabel;

type
  TFCOI0010P3 = class(TForm)
    PnlButton: TfsPanel;
    BtnOpen: TfsButton;
    BtnApply: TfsButton;
    BtnInit: TfsButton;
    CDS1: TClientDataSet;
    DS1: TDataSource;
    BtnClose: TfsButton;
    DBGrid1: TcxGrid;
    DBGridView1: TcxGridDBBandedTableView;
    DBGridView1Column1: TcxGridDBBandedColumn;
    DBGridView1Column2: TcxGridDBBandedColumn;
    DBGridView1Column3: TcxGridDBBandedColumn;
    DBGridView1Column4: TcxGridDBBandedColumn;
    DBGridView1Column5: TcxGridDBBandedColumn;
    DBGridLevel1: TcxGridLevel;
    Edit1: TEdit;
    PnlSearch: TfsPanel;
    LblUSERIDNM: TfsLabel;
    EdtUSERIDNM: TcxTextEdit;
    FrmUSERGBCD: TFrmChkCombo;
    FrmCIRCULATION: TFrmChkCombo;
    FrmCHANNEL: TFrmChkCombo;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormDestroy(Sender: TObject);
    procedure DBGridView1DblClick(Sender: TObject);
    procedure BtnOpenClick(Sender: TObject);
    procedure EdtSTORECDNMPropertiesChange(Sender: TObject);
    procedure OpenCDS;
    procedure BtnInitClick(Sender: TObject);
    procedure BtnApplyClick(Sender: TObject);
    procedure FrmCHARGEUSEREdtCodePropertiesChange(Sender: TObject);
    procedure FrmSHOPCDEdtCodePropertiesChange(Sender: TObject);
    procedure BtnCloseClick(Sender: TObject);
    procedure EdtUSERIDNMKeyPress(Sender: TObject; var Key: Char);
    procedure FrmUSERGBCDCmbCodePropertiesEditValueChanged(Sender: TObject);
    procedure DBGridView1Column1HeaderClick(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    Global: IGlobal;
    procedure Init;
  end;

var
  FCOI0010P3: TFCOI0010P3;

implementation

uses UDataModuleClient;

{$R *.dfm}


procedure TFCOI0010P3.Init;
begin
//  Global.CheckComboBoxAddItem(FrmBRAND.CmbCode, Format(Global.GetSql('CBSQL_BRAND' ),[Global.GetSysParam('DATAAREAID'), '', Global.StrToInStr('')]));
  FrmUSERGBCD.AddItems(Format(Global.GetSql('CBSQL_CODE'), ['SY', '00410', 'Y']));
  FrmCIRCULATION.AddItems(Format(Global.GetSql('CBSQL_CODE'), ['SA', '00110', 'Y']));
  FrmCHANNEL.AddItems(Format(Global.GetSql('CBSQL_CODE'), ['SA', '00130', 'Y']));

  Global.ImageComboBoxAddItem(TcxImageComboBoxProperties(DBGridView1.GetColumnByFieldName('USERGBCD').Properties),
    Format(Global.GetSql('CBSQL_CODE'), ['SY', '00410', '']));

end;

procedure TFCOI0010P3.FormCreate(Sender: TObject);
begin
//
end;

procedure TFCOI0010P3.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

  if Shift = [] then
  begin
    case Key of
      vk_F3     : if BtnOpen.Enabled  then BtnOpen.Click;
      vk_F12    : if BtnInit.Enabled  then BtnInit.Click;
//      VK_RETURN : Perform(CM_DIALOGKEY, VK_TAB, 0);
      VK_ESCAPE : if BtnClose.Enabled  then BtnClose.Click;
    end;
  end;

end;

procedure TFCOI0010P3.FrmCHARGEUSEREdtCodePropertiesChange(Sender: TObject);
begin
  BtnInitClick(nil);
end;

procedure TFCOI0010P3.FrmSHOPCDEdtCodePropertiesChange(Sender: TObject);
begin
  BtnInitClick(nil);
end;

procedure TFCOI0010P3.FrmUSERGBCDCmbCodePropertiesEditValueChanged(
  Sender: TObject);
begin
  // EditChangedEvent(False);
  try
    if CDS1.Active then
      CDS1.Close;

    if Sender = nil then
    begin
      Global.ClearControls(PnlSearch);
      Global.FirstControlSetFocus(Self);
    end;
  finally
    EdtUSERIDNM.Clear;
  end;

end;

procedure TFCOI0010P3.FormDestroy(Sender: TObject);
begin
  CDS1.Close;
end;

procedure TFCOI0010P3.DBGridView1Column1HeaderClick(Sender: TObject);
begin
  Global.ChoiceAndCancelGridData(Sender);
end;

procedure TFCOI0010P3.DBGridView1DblClick(Sender: TObject);
begin
  if (Not CDS1.Active ) or (CDS1.IsEmpty) then Exit;

  ModalResult := mrOk;
end;

procedure TFCOI0010P3.BtnApplyClick(Sender: TObject);
begin
  if (Not CDS1.Active ) or (CDS1.IsEmpty) then Exit;

  ModalResult := mrOk;
end;

procedure TFCOI0010P3.BtnCloseClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TFCOI0010P3.BtnInitClick(Sender: TObject);
begin
  CDS1.Close;

  if Assigned(Sender) then
  begin
    Global.ClearControls(PnlSearch);
    Global.FirstControlSetFocus(PnlSearch);
  end;
end;

procedure TFCOI0010P3.BtnOpenClick(Sender: TObject);
begin
  Global.ChkFrameValue(PnlSearch);

  Global.StartJob;

  CDS1.DisableControls;
  try

    OpenCDS;

    if CDS1.IsEmpty then
    begin
      Global.ShowCustomMessage(Global.GetMessage('msg_Q03'), 3);
      Global.FirstControlSetFocus(PnlSearch);
      Exit;
    end;

  finally
    CDS1.EnableControls;

//    if Not CDS1.IsEmpty then
//    begin
//      DBGridView1.DataController.FocusedRowIndex := 0;
//      DBGridView1.DataController.SelectRows(0,0);
//    end;

  end;
  DBGrid1.SetFocus;

end;

procedure TFCOI0010P3.OpenCDS;
var
  subSQL  : string;
begin

  Global.StartJob;
  Global.SQLConnection.Connected := True;

  try
    with CDS1 do
    begin
      Close;
      CommandText := Format(Global.GetSql('OPENP3', COPY(Self.Name,1,8)), [
        Global.GetSysParam('COMPANYCD')
        , GLOBAL.GetCheckedShortDescription(FrmUSERGBCD.CmbCode,false)
        , GLOBAL.GetCheckedShortDescription(FrmUSERGBCD.CmbCode)
        , ''//FrmUSERIDNM.EdtCode.Text
        , EdtUSERIDNM.Text
        , GLOBAL.GetCheckedShortDescription(FrmCIRCULATION.CmbCode,false)
        , GLOBAL.GetCheckedShortDescription(FrmCIRCULATION.CmbCode)
        , GLOBAL.GetCheckedShortDescription(FrmCHANNEL.CmbCode,false)
        , GLOBAL.GetCheckedShortDescription(FrmCHANNEL.CmbCode)
        ]);

EDIT1.Text := COMMANDTEXT;
      Open;

      if IsEmpty then
      begin
        Global.ShowCustomMessage(Global.GetMessage('MSG_Q03'), 3);
        exit;
      end;
    end;

  finally
    Global.SQLConnection.Connected := False;
  end;


end;

procedure TFCOI0010P3.EdtSTORECDNMPropertiesChange(Sender: TObject);
begin
    if CDS1.Active then CDS1.Close;
end;

procedure TFCOI0010P3.EdtUSERIDNMKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    if Sender = FrmCHANNEL.CmbCode then BtnOpen.Click
    else GetParentForm(TControl(Sender)).Perform(CM_DIALOGKEY, VK_TAB, 0);
  end;
end;

end.
